package avicit.bdp.dms.tdm.service;

import avicit.bdp.common.base.BaseService;
import avicit.bdp.dms.tdm.dao.BdpAssetMountFileDao;
import avicit.bdp.dms.tdm.dto.BdpAssetDTO;
import avicit.bdp.dms.tdm.dto.BdpAssetMountFileDTO;
import avicit.platform6.commons.utils.ComUtil;
import avicit.platform6.commons.utils.PojoUtil;
import avicit.platform6.core.exception.DaoException;
import avicit.platform6.core.properties.PlatformConstant;
import avicit.platform6.modules.system.syslog.service.SysLogUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BdpAssetMountFileService
    extends BaseService<BdpAssetMountFileDao, BdpAssetMountFileDTO> {

  @Autowired
  private BdpAssetService assetService;

  /**
   * 通过主键查询单条记录
   *
   * @param id 主键id
   */
  @Transactional(readOnly = true)
  public BdpAssetMountFileDTO queryById(String id) {
    try {
      BdpAssetMountFileDTO dto = this.selectByPrimaryKey(id);
      // 记录日志
      if (dto != null) {
        SysLogUtil.log4Query(dto);
      }
      return dto;
    } catch (Exception e) {
      throw new DaoException(e.getMessage(), e);
    }
  }

  /**
   * 新增对象
   *
   * @param dto 保存对象
   * @return String
   */
  @Transactional
  public String save(BdpAssetMountFileDTO dto) {
    try {
      dto.setId(ComUtil.getId());
      PojoUtil.setSysProperties(dto, PlatformConstant.OpType.insert);
      dto.setDatasourceId(dto.getProjectId());
      this.insert(dto);
      if (StringUtils.isNotBlank(dto.getOpenServiceType())){
        BdpAssetDTO assetDTO = assetService.selectByPrimaryKey(dto.getAssetId());
        assetDTO.setOpenServiceType(dto.getOpenServiceType());
        assetService.updateByPrimaryKey(assetDTO);
      }
      // 记录日志
      SysLogUtil.log4Insert(dto);
      return dto.getId();
    } catch (Exception e) {
      throw new DaoException(e.getMessage(), e);
    }
  }

  /**
   * 修改对象部分字段
   *
   * @param dto 修改对象
   * @return int
   */
  @Transactional
  public int update(BdpAssetMountFileDTO dto) {
    try {
      int count = this.updateByPrimaryKey(getUpdateDto(dto));
      if (StringUtils.isNotBlank(dto.getOpenServiceType())){
        BdpAssetDTO assetDTO = assetService.selectByPrimaryKey(dto.getAssetId());
        assetDTO.setOpenServiceType(dto.getOpenServiceType());
        assetService.updateByPrimaryKey(assetDTO);
      }
      if (count == 0) {
        throw new DaoException("数据失效，请重新更新");
      }
      return count;
    } catch (Exception e) {
      throw new DaoException(e.getMessage(), e);
    }
  }

  /** 内部方法，获取修改的dto对象 */
  private BdpAssetMountFileDTO getUpdateDto(BdpAssetMountFileDTO dto) {
    BdpAssetMountFileDTO oldDTO = selectByPrimaryKey(dto.getId());
    if (oldDTO == null) {
      throw new DaoException("数据不存在");
    }
    // 记录日志
    SysLogUtil.log4Update(dto, oldDTO);
    PojoUtil.setSysProperties(dto, PlatformConstant.OpType.update);
    PojoUtil.copyProperties(oldDTO, dto, true);
    oldDTO.setDatasourceId(dto.getProjectId());
    return oldDTO;
  }

  public BdpAssetMountFileDTO queryByAssetId(String assetId) {
    return this.mapper.queryByAssetId(assetId);
  }
}
